<?php

set_time_limit(0);

// --- convert: where the actual conversion takes place --- //
function convert($filename) {
	global $counter, $HTTP_POST_VARS; 

	$xml = new XMLFile();

	
	if ( (strpos($filename, "andard") > 1) || (strpos($filename, "ntry") > 1) ) {
		// name is something like 'standard-00000', so go ahead
		$fh = fopen($filename, 'r' );
		if ($xml->read_file_handle($fh)) {
			// only if we could parse it as something that resembles an entry, we insert it
			$row_entrypoint= &$xml->roottag->tags[0];
			$row=&$xml->roottag->tags[0]->make_array();

			if (isset($HTTP_POST_VARS['keepuid'])) {
				$row['code'] = $xml->roottag->tags[0]->attributes['CODE'];
				$row['code'] = str_replace("blog-", "", $row['code']);
			} else {
				$row['code'] = "";
			}

			// fix some things..
			$row['comments'] = $row['comment'];
			unset($row['comment']);
			if (count($row['comments'])) {
				foreach($row['comments'] as $key => $comment) {
					$row['comments'][$key]['comment'] = escape($row['comments'][$key]['comment']); 
				}
			}

			$row['title'] = escape ($row['title']);
			$row['introduction'] = escape (nl2br($row['introduction']));
			$row['body'] = escape (nl2br($row['body']));

			fclose( $fh );
			unset($xml);
			
			echo "conv: $filename ";

			if (insert_entry($row)) {
				// Entry has been inserted, so we delete it..
				if (isset($HTTP_POST_VARS['unlink'])) {
					unlink($filename);
				}

			} 
		} else {
			// even if we couldn't parse it, we still need to close it
			fclose( $fh );
			unset($xml);
		}
	}

}


// --- insert_entry: Puts the function into the 1.0 db--- //
function insert_entry($entry) {
	global $db;

	if ($entry['code']=="") {
		$entry['code']=">";
	}

	if ( ($entry['category'] == "standard") || ($entry['category'] == "") ) { 
		$entry['category'] = "default"; 
	}
	$entry['category'] = array($entry['category']);



	$entry = $db->set_entry($entry);
	if ($db->save_entry()) {
		echo "(inserted!)<br />";
		return TRUE;
	} else {
		echo "(<b>NOT</b> inserted!)<br />";
		return FALSE;
	}


}

// -------
function start_conversion($dir) {
	global $db;

	// open the db, make sure it's updated..
	$db = new db();
	$db->generate_index();

	$dir= realpath($dir);

	$d= dir($dir);

	while ($filename=$d->read()) {

		if ( ($filename=="..") || ($filename==".") ) { continue; }

		if (is_dir($dir."/".$filename)) {
			

			$d2= dir($dir."/".$filename);
			while ($filename2=$d2->read()) {
				if ( is_dir($dir."/".$filename."/".$filename2) || ($filename2=="..") || ($filename2==".") ) { 
					continue; 
				}
				convert($dir."/".$filename."/".$filename2);
				flush();
			}
			$d2->close();

		} else {
			
			convert($dir."/".$filename);

		}

		flush();

	}

	$d->close();

	echo "<br /><br />\n\n<b>conversion completed in ".timetaken()." seconds.</b><br /><br />\n";
	echo "<a href='index.php'>Log in to Pivot..</a>.</div>";

}


function show_form() {
	global $HTTP_SERVER_VARS, $HTTP_POST_VARS;

	$self = $HTTP_SERVER_VARS['PHP_SELF'];
	$dir = isset($HTTP_POST_VARS['dir']) ? $HTTP_POST_VARS['dir'] : "" ;

	echo "The current path is: ".dirname($self)."<br /><br />";

	echo "Please give the location of your old pivot's db folder. <br />Depending on where your previous install is located, this can look something like '../../pivot/db/'.<br /><br />";
	echo "<b>Location:</b> <form method='post' action='$self'><input type='text' name='dir' size='30' value='$dir' /><br /><br />";

	echo "<input type='checkbox' name='unlink' value='1' /> <b>Remove old entries after conversion?</b><br /><small>If you're server runs in safe_mode and you can't convert all entries in one go, use this option. This way the converted old entries will be removed. And you can restart the conversion to continue where it stopped.</small><br /><br />";

	echo "<input type='checkbox' name='keepuid' value='1' /> <b>Keep original uid/numbers?</b><br /><small>If you choose to keep the original UID's, the original numbering of entries will remain the same. Otherwise the converted entries will be added to the new database and be assigned a new number (this is the prefered option).</small><br /><br />";



	echo "<input type='checkbox' name='confirm' value='1' /> <b>Yes, do it!</b> <br /><br />";
	echo "<input type='submit' value='Convert!' /></form>";

}


// -------- Main ----------

?>

<h1>Welcome to the quick-and-dirty conversion script</h1>
<h3>Use this to convert entries from Pivot 0.10, 0.11 or 0.12 to Pivot 1.0.</h3>

<?php

if (count($HTTP_POST_VARS)>0) {

	// we know there's input..

	if (strlen($HTTP_POST_VARS['dir'])<3) {

		// no dir given
		echo "<b>You need to give the location of your old Pivot's db/ folder..</b><br /><br />";
		show_form();

	} else if (!is_dir(realpath($HTTP_POST_VARS['dir']))) {

		// not a dir..
		echo "<b>The dir you've given translates to: <br />";
		echo realpath($HTTP_POST_VARS['dir'])."<br />";
		echo "This folder does not exist.. Please verify your input.</b><br /><br />";
		show_form();

	} else if (!isset($HTTP_POST_VARS['confirm'])) {

		// not confirmed..
		echo "<b>You need to confirm that you want to start the conversion..</b><br /><br />";
		show_form();

	} else {

		// go!

		include_once("modules/old_module_xml.php");
		include_once("modules/old_xmlfile.php");
		include_once("pv_core.php");



		start_conversion($HTTP_POST_VARS['dir']);

	}



} else {

	show_form();

}


?>

</BODY>
</HTML>
